package utils;



import org.apache.commons.mail.util.MimeMessageParser;
import org.apache.commons.mail.util.MimeMessageUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: 一平
 * Date: 14-5-30
 * Time: 上午9:09
 * To change this template use File | Settings | File Templates.
 */
public class EmailTest {
    public static void main(String[] args) {
        File file = new File("d:/test/测试2+图片_.eml");
        try {
            MimeMessage mimeMessage = MimeMessageUtils.createMimeMessage(null, file);
            MimeMessageParser parser = new MimeMessageParser(mimeMessage);
            System.out.println("发件人："+parser.getFrom());
            System.out.println("主题："+parser.getSubject());
            System.out.println("收件人："+parser.getTo());

            //parser.parse().getMimeMessage().getInputStream();
            System.out.println(parser.parse().getPlainContent());  //结果为空
            System.out.println(parser.parse().getHtmlContent()); //结果为空
            //parse.parse()方法返回的也是MimeMessageParser对象，不调用parse()方法就无法的到邮件内容，只能得到主题和收件人等信息
            if(parser.parse().hasPlainContent()){  //这里一定要调用parser.parse方法
                System.out.println(parser.parse().getPlainContent());
            }



        } catch (MessagingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }
}
